package com.free.instadownload;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.free.instadownload.adapter.MyPagerAdapter;
import com.free.instadownload.fragment.BookmarkFragment;
import com.free.instadownload.fragment.DownloadFragment;
import com.free.instadownload.fragment.RecentDownloadFragment;

public class MainActivity extends FragmentActivity implements OnPageChangeListener, OnClickListener{
	private ViewPager viewPager;
	private TextView tvBookmark, tvDownload, tvRecent;
	private MyPagerAdapter pagerAdapter;
	private List<Fragment> fragments;
	private int currentPage = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		viewPager = (ViewPager) findViewById(R.id.viewPager);
		tvBookmark = (TextView) findViewById(R.id.tvBookmark);
		tvDownload = (TextView) findViewById(R.id.tvDownload);
		tvRecent = (TextView) findViewById(R.id.tvRecent);
		pagerAdapter = new MyPagerAdapter(getSupportFragmentManager(), getListFragment());
		viewPager.setAdapter(pagerAdapter);
		viewPager.setOnPageChangeListener(this);
		tvBookmark.setOnClickListener(this);
		tvDownload.setOnClickListener(this);
		tvRecent.setOnClickListener(this);
	}
	
	protected List<Fragment> getListFragment(){
		fragments = new ArrayList<Fragment>();
		fragments.add(BookmarkFragment.create(0));
		fragments.add(DownloadFragment.create(1));
		fragments.add(RecentDownloadFragment.create(2));
		return fragments;
	}
	
	private void gotoPage(int page) {
		viewPager.setCurrentItem(page, true);
		currentPage = page;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.tvBookmark:
			gotoPage(0);
			break;
		case R.id.tvDownload:
			gotoPage(1);
			break;
		case R.id.tvRecent:
			gotoPage(2);
			break;
		default:
			break;
		}
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPageSelected(int arg0) {
		// TODO Auto-generated method stub
		if (arg0 != currentPage) {
			switch (arg0) {
			case 0:
				tvBookmark.setBackgroundColor(Color.parseColor("#10214c"));
				tvDownload.setBackgroundColor(Color.WHITE);
				tvRecent.setBackgroundColor(Color.WHITE);
				break;
			case 1:
				tvBookmark.setBackgroundColor(Color.WHITE);
				tvDownload.setBackgroundColor(Color.parseColor("#10214c"));
				tvRecent.setBackgroundColor(Color.WHITE);
				break;
			case 2:
				tvBookmark.setBackgroundColor(Color.WHITE);
				tvDownload.setBackgroundColor(Color.WHITE);
				tvRecent.setBackgroundColor(Color.parseColor("#10214c"));
				break;
			default:
				break;
			}
		}
		currentPage = arg0;
	}
}